import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from "@nestjs/common";
import { Request,Response } from "express";

@Catch(HttpException)
export class HttpExceptionFilters implements ExceptionFilter {
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();
        const status = exception.getStatus();
        const exceptionRes: any = exception.getResponse();
        const{
            error,
            message,
        } = exceptionRes;


        response.status(status).json({
            status: status,
            timestamp: new Date().toISOString(),
            path: request.url,
            error,
            message,
        });
    }
}